package com.kaka.bus.adapter;

import android.content.Context;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.kaka.bus.R;
import com.kaka.bus.bean.MessageBean;
import com.kaka.bus.constant.Constant;
import com.kaka.bus.util.Utils;

import java.util.List;

/**
 * Created by zhangqi on 2016/11/2.
 */

public class MessageAdapter extends BaseQuickAdapter<MessageBean,BaseViewHolder> {

    private Context mContext;
    public MessageAdapter(int layoutResId, @Nullable List<MessageBean> data, Context context) {
        super(layoutResId, data);
        mContext = context;
    }

    @Override
    protected void convert(BaseViewHolder helper, MessageBean item) {
        helper.setText(R.id.tv_message_content,"\u3000\u3000"+item.getContent())
                .setText(R.id.tv_message_time,item.getCreate_time())
                .setText(R.id.iv_message_state,item.getTitle())
                .setVisible(R.id.iv_message_unread, !Constant.MESSAGE_YES.equals(item.getStatus()));
        helper.itemView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
            @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN)
            @Override
            public void onGlobalLayout() {
                if(item.isOpen()){
                    TextView message = helper.getView(R.id.tv_message_content);
                    ImageView mohu = helper.getView(R.id.tv_message_mohu);
                    message.setMaxLines(message.getLineCount());
                    mohu.setVisibility(View.GONE);
                }else{
                    TextView message = helper.getView(R.id.tv_message_content);
                    ImageView mohu = helper.getView(R.id.tv_message_mohu);
                    message.setMaxLines(1);
                    mohu.setVisibility(View.VISIBLE);
                }
                helper.itemView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });

        if(helper.getAdapterPosition() == mData.size() - 1){
            ViewGroup.MarginLayoutParams params = (ViewGroup.MarginLayoutParams) helper.itemView.getLayoutParams();

            params.bottomMargin = Utils.dp2px(mContext,15);
            helper.itemView.setLayoutParams(params);
        }
    }
}
